﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//
using System.Data.SqlClient;

public partial class buy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        object carID = Server.UrlDecode(Request.QueryString["carID"]);
        if (carID != null)
        {
            Session["carID"] = carID.ToString().Trim();
            txbcar.Enabled = false;
            txbcar.Text = carID.ToString().Trim();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = txbname.Text.Trim();
        string sexs = sex.Text.Trim();
        string address = txbaddress.Text.Trim();
        string tel = txbtel.Text.Trim();
        string date = txbdate.Text.Trim();
        string cars = txbcar.Text.Trim();
        string Tim = DateTime.Now.ToString("yyyy年MM月dd日 hh时mm分ss秒");
        DateTime tims = DateTime.Now;
        if (name == "")
        {
            Response.Write("<script>alert('姓名不能为空！')</script>");
            txbname.Focus();
            return;
        }
        else if (name.Length > 4 || name.Length == 1)
        {
            Response.Write("<script>alert('请输入正确的姓名！')</script>");
            txbname.Focus();
            return;
        }
        else if (sexs == "")
        {
            Response.Write("<script>alert('性别不能为空！')</script>");
            return;
        }
        else if (tel == "")
        {
            Response.Write("<script>alert('联系方式不能为空！')</script>");
            txbtel.Focus();
            return;
        }
        else if (tel.Length != 11)
        {
            Response.Write("<script>alert('请输入正确的手机号码！')</script>");
            txbtel.Focus();
            return;
        }
        else if (address == "")
        {
            Response.Write("<script>alert('请输入正确的联系地址！')</script>");
            txbaddress.Focus();
            return;
        }
        else if (date == "")
        {
            Response.Write("<script>alert('请输入正确的交货日期！')</script>");
            txbdate.Focus();
            return;
        }
        else if (cars == "")
        {
            Response.Write("<script>alert('请输入预约车型！')</script>");
            txbcar.Focus();
            return;
        }
        else
        {
            string sql = "insert into buy_info values('" + name + "','" + sexs + "','" + tel + "','" + address + "','" + date + "','" + cars + "','" + Tim + "','"+tims+"')";
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
            try
            {
                int count = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);
                if (count != 0)
                {
                    Response.Write("<script>alert('感谢您的使用。我们将以最快的速度和您联系！')</script>");
                    return;
                }
                else
                {
                    Response.Write("<script>alert('操作失败！')</script>");
                    return;
                }
            }
            catch
            {
                Response.Write("<script>alert('系统正忙，请稍后重试！')</script>");
                return;
            }

        }
    }
}
